<?php

namespace common\entities\common;

use common\entities\ConfigEntity;
use Yii;

/**
 * This is the model class for table "myscrm_organizationtoauthorize".
 *
 * @property string $Id
 * @property string $OrganizationId
 * @property string $ApplicationId
 * @property string $AuthorizeTypeId
 * @property integer $TotalNum
 * @property string $VersionNumber
 * @property string $ContractId
 * @property integer $IsQdAdvanced
 */
class MyscrmContractNumToAdDrainEntity extends ConfigEntity
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'myscrm_ad_drain';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Money', 'BackupsMoney'], 'double'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'Id' => 'ID',
            'Time' => '广告消费记录增加点时间',
            'Money' => '广告消费点金额',
            'DownloadUrl' => '预览PDF',
            'AdDrainId' => '与合同关联id',
        ];
    }
    
    /**
    * 根据合同号,获取广告列表
     * // TODO 广告列表
    * @param string $organizationId 租户id
    * @param string $contractId 合同id
    * @return array
    */
	public static function getAdDrainListByContractId($AdDrainId) {

		//return self::find()->select(['Id','Time','Money','FileName'])->where($arr)->all();
		return self::find()->asArray()->where(['AdDrainId'=>$AdDrainId,'IsDel'=>2])->all();
	}

    /**
     * 根据合同号,增加广告记录
     * // TODO 增加广告记录
     * @param string $organizationId 租户id
     * @param string $contractId 合同id
     * @return array
     */
    public static function addAdDrainListByContractId($lists,$AdDrainId) {

        $status = null;

        foreach ($lists as $list) {
            $addAdList = new MyscrmContractNumToAdDrainEntity();
            //$addAdList->Time = $list['Time'];
            $addAdList->startOn = $list['startOn'];
            $addAdList->endOn = $list['endOn'];
            $addAdList->EstablishTime = time();
            $addAdList->Money = (float)$list['Money'];
            $addAdList->BackupsMoney = (float)$list['Money'];

            $addAdList->DownloadUrl = $list['DownloadUrl'];
            $addAdList->BackupsUrl = $list['DownloadUrl'];

            if(!empty($list['DownloadUrl'])){//防止有问题的文件上传也会显示图片名称
                $addAdList->FileName = $list['FileName'];
            }

            $addAdList->SaveName = $list['SaveName'];
            $addAdList->AdDrainId = $AdDrainId;

            $addAdList->save();
            $status = $addAdList->attributes['Id'];
        }

        return $status;
    }

    /**
     * 根据合同号,以及广告id修改广告记录
     * // TODO 修改广告记录
     * @param string $organizationId 租户id
     * @param string $contractId 合同id
     * @return array
     */
    public static function editAdDrainListByContractId($contractId,$arr=array(),$idToDelpath=null) {

        $status = null;


        foreach ($arr as $list) {

            $ad = self::find()->where(['Id'=>$list['Id']])->one();

            //sonar 坏味道修复

            if(!empty($list['startOn'])){
                $ad->startOn = $list['startOn'];
            }

            if(!empty($list['endOn'])){
                $ad->endOn = $list['endOn'];
            }

            if(!empty($list['Money'])){
                $ad->Money = (float)$list['Money'];
                $ad->BackupsMoney = (float)$list['Money'];
            }

            if($list['Money'] === '0'){
                $ad->Money = (float)$list['Money'];
                $ad->BackupsMoney = (float)$list['Money'];
            }

            if(!empty($list['DownloadUrl'])){
                $ad->DownloadUrl = $list['DownloadUrl'];
                $ad->BackupsUrl = $list['DownloadUrl'];
            }

            if(!empty($list['SaveName'])){
                $ad->SaveName = $list['SaveName'];
            }

            if(!empty($list['FileName'])){
                $ad->FileName = $list['FileName'];
            }

            if($ad){
                $ad->ModifiedOn = date('Y-m-d H:i:s');
                $ad->save();
                $status = $ad->attributes['Id'];
            }

        }

        if($idToDelpath){
            $ad = self::find()->where(['Id'=>$idToDelpath])->one();
            $ad->DownloadUrl = '';
            $ad->SaveName = '';
            $ad->FileName = '';
            $ad->save();
            $status = $ad->attributes['Id'];
        }


        return $status;
    }

    /**
     * 根据合同号,以及广告id修改广告记录
     * // TODO 删除广告记录
     * @param string $organizationId 租户id
     * @param string $contractId 合同id
     * @return array
     */
    public static function delAdDrainListByContractId($id,$contractId) {

        $status = null;

        $ad = self::find()->where(['Id'=>$id])->one();

        if($ad){
            $ad->IsDel = 1;
            $ad->save();
        }

        $status = $ad->attributes['Id'];
        return $status;
    }
    
}
